import json
import re

from curl_cffi import requests
from util.headers import get_header
from util.filessave import save_data
root =save_data('商品')
from lxml import etree
url = 'https://pro.jd.com/mall/active/8tHNdJLcqwqhkLNA8hqwNRaNu5f/index.html'
res = requests.get(url,headers=get_header('https://pro.jd.com/mall/active/2cqCAHDDWm2f5u5irY4TsPPWHQNj/index.html?'))
# print(res.text)
# tree = etree.HTML(res.text.encode())
# data =tree.xpath('//script[contains(text(), "__react_data__")]/text()')[0]
# # print(data)
# dic_data=re.search(r"\(function\(\) \{.*?window\.__react_data__ =(.*?) .*?}\(\)\);",data,re.S)
# pri
html = res.text

# 1. 切出 JSON 字符串
json_str = re.search(r'window\.__react_data__\s*=\s*({.+?})\s*;', html, re.S).group(1)
data = json.loads(json_str)

# 2. 递归扫描所有 dict 里的 picUrl0
def iter_pic_url0(obj):
    if isinstance(obj, dict):
        if 'picUrl0' in obj:
            yield obj['picUrl0']
        for v in obj.values():
            yield from iter_pic_url0(v)
    elif isinstance(obj, list):
        for item in obj:
            yield from iter_pic_url0(item)

pic_list = list(iter_pic_url0(data))


for i,img_data in enumerate(iterable=pic_list):
    print(i,img_data)
    res_img = requests.get(f'https:{img_data}').content

    with open(f"{root}{i}_img.jpg",'wb')as f:
        f.write(res_img)
        print(f"第{i}张照片爬取完毕")

# rss = requests.get('https://m.360buyimg.com/babel/jfs/t1/350858/9/16510/30162/68f9b001F72df9c19/ca6f788ff6f69a81.jpg')
# print(rss.status_code)
